package query

import (
	"strings"

	"ssalarm/common/dto"
)

type AlarmReportReq struct {
	TopSize      int    `form:"topSize" search:"-"`
	Type         string `form:"type" search:"-"`
	DeptId       string `form:"deptId" search:"-"`
	VariableName string `form:"variableName" search:"-"`
	TimeStamp    string `form:"timeStamp" search:"-"`

	StartTime  string   `form:"startTime" search:"type:gte;column:alarm_time;table:sys_alarm"`
	EndTime    string   `form:"endTime" search:"type:lte;column:alarm_time;table:sys_alarm"`
	AlarmType  string   `form:"alarmType" search:"type:exact;column:alarm_type;table:sys_alarm"`
	AlarmLevel *int     `form:"alarmLevel" search:"type:exact;column:level;table:sys_alarm"`
	Sn         string   `form:"sn" search:"type:exact;column:dev_sn;table:sys_alarm"`
	DeptIdIn   []string `form:"depIdIn" search:"type:in;column:dept_id;table:sys_alarm"`

	AlarmListParam
	dto.OrderBy
	dto.UserInfo
}

func (m *AlarmReportReq) GetNeedSearch() interface{} {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DeptIdIn = m.UserInfo.DeptIdList
		}
	} else {
		m.DeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	if len(m.StartTime) == 0 && len(m.TimeStamp) > 0 {
		day := strings.Split(m.TimeStamp, "~")
		m.StartTime = strings.Trim(day[0], " ")
		m.EndTime = strings.Trim(day[1], " ")
	}

	return *m
}

func (m *AlarmReportReq) GetDeptIdList() []string {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DeptIdIn = m.UserInfo.DeptIdList
		}
	} else {
		m.DeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	return m.DeptIdIn
}

type AlarmListParam struct {
	IdList           []string `search:"type:in;column:id;table:sys_alarm"`
	AlarmTypeList    []string `search:"type:in;column:alarm_type;table:sys_alarm"`
	VariableNameList []string `search:"type:in;column:var_name;table:sys_alarm"`
	DeviceSnList     []string `search:"type:in;column:dev_sn;table:sys_alarm"`
	DeptIdList       []string `form:"deptIdList" search:"type:in;column:dept_id;table:sys_alarm"`
}

type AlarmTotalData struct {
	Name  string
	Count int64
}
